<?
/*
 * qq login function
 * uther 2012.8.16 start
 */
class qq{
	var $qq_config = array (
	  'appid' => '100297249',
	  'appkey' => '2b7d1d460bf9bd3593f99d8db37df02d',
	  'api' => array (
			    		'get_user_info' => '1'
			  		),
	  'debug' => '0',
	);
	
	function qq_login(){
		$_SESSION['qq_login_state'] = md5(uniqid(mt_rand(), true));
		$return_url = 'http://www.geili0574.com/index.php/login/qq_back';
		$_SESSION['qq_redirect_url'] = $return_url;
		$parms = array(
					    "response_type"    => "code",
					    "client_id"        => $this->qq_config['appid'],
					    "redirect_uri"     => $_SESSION['qq_redirect_url'],
					    "scope"            => join(",", array_keys($this->qq_config['api'])),
					    "state"            => $_SESSION['qq_login_state']
					);
		$u = new union;
		$url = "https://graph.qq.com/oauth2.0/authorize?" . $u->build_request_str($parms, true);
		header("Location: ".$url);exit;
	}
	
	
	function qq_get_token(){
		if (empty($_GET['code'])) return false;
		
		if ($_GET['state'] != $_SESSION['qq_login_state']){
			return false;
		}
		$url = "https://graph.qq.com/oauth2.0/token";
		$parms = array(
			"grant_type"    =>    "authorization_code",
			"client_id"        =>    $this->qq_config['appid'],
			"client_secret"    =>    $this->qq_config['appkey'],
			"code"            =>     trim($_GET['code']),
			"state"            =>    $_SESSION["qq_login_state"],
			"redirect_uri"    =>    $_SESSION['qq_redirect_url']
		);
		$ret = $this->get($url, $parms);
		
		unset($_SESSION["qq_login_state"], $_SESSION["qq_redirect_url"]);
		
		if($ret !== false){
			parse_str($ret, $result);
			
			return $result;
		
		}
		return false;
	}
	function qq_get_openid($token = ''){
	
		if (empty($token))return false;
		
		$url = "https://graph.qq.com/oauth2.0/me";
		$ret = $this->get($url, array("access_token" => $token));
		
		if($ret !== FALSE){
			$ret_str = preg_replace('/callback\(\s+(.*?)\s+\).*/i', '$1', $ret);
			
			if (!empty($ret_str)){
				$result = json_decode($ret_str, true);
				return trim($result['openid']);
			}
		}
		return false;
	}
	function qq_get_info($token = '', $openid = ''){
		
		$params = array(
						"access_token"    => $token,
						'openid' => $openid,
						'oauth_consumer_key' => $this->qq_config["appid"]
						);
		$url = "https://graph.qq.com/user/get_user_info";
		$ret = $this->get($url, $params);
		$ret = str_replace("\\x","&#x",$ret );
		if($ret !== false){
			$result = json_decode($ret, true);
			return $result;
		}
		return false;
	}
	
	
	
	
	
	function get($url, $input = array()){
		if (empty($url)) return;
		
	    $curl = curl_init();
	    if(stripos($url,"https://")!==FALSE){
	        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	    }
	    $u = new union;
	    curl_setopt($curl, CURLOPT_URL, $url."?".$u->build_request_str($input));
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
	    $result = curl_exec($curl);
	    $info = curl_getinfo($curl);
	    curl_close($curl);
	    return $result;exit;
	    if(intval($this->qq_config["debug"])===1){
	        echo "<tr><td class='narrow-label'>请求地址:</td><td><pre>".$url."</pre></td></tr>";
	        echo "<tr><td class='narrow-label'>GET参数:</td><td><pre>".var_export($input,true)."</pre></td></tr>";
	        echo "<tr><td class='narrow-label'>请求信息:</td><td><pre>".var_export($info,true)."</pre></td></tr>";
	        if(intval($info["http_code"])==200){
	            echo "<tr><td class='narrow-label'>返回结果:</td><td><pre>".$result."</pre></td></tr>";
	            if((@$r = json_decode($result,true))){
	                echo "<tr><td class='narrow-label'>结果集合解析:</td><td><pre>".var_export($r,true)."</pre></td></tr>";
	            }
	        }
	    }
	    if(intval($info["http_code"])==200){
	        return $result;
	    }else{
	        echo "<tr><td class='narrow-label'>返回出错:</td><td><pre>".$info["http_code"].",请检查参数或者确实是腾讯服务器出错咯。</pre></td></tr>";
	        return FALSE;
	    }
	}
}	
?>